#ifndef VOIP_THREAD_POOL_H_
#define VOIP_THREAD_POOL_H_
#include <map>
#include "ct_voip.h"
#include "ct_noncopyable.h"
#include "ct_mutex.h"
#include "ctSocket.h"
#include "ct_thread_group.h"

class ctVOIPRSThread;
class ctVOIPRelayThread;
class ctVOIPTCPTask;
class ctVOIPTCPConnectThread;
class ctVOIPThreadPool:
        private ctNoncopyable
{
        public:
                explicit ctVOIPThreadPool() 
                {
                        rs_thread_rtp_ = NULL;
                        rs_thread_rtcp_= NULL;
                        relay_thread_rtp_ = NULL;
                        relay_thread_rtcp_= NULL;
                        tcp_connect_thread_ = NULL;
                }
                ~ctVOIPThreadPool()
                {
                        final();
                }

                bool init(struct stVOIPConfig &);
                void final();
                void setRelayServerAddr(const std::string &ip, const WORD &port);
                bool connectRelayServer();

                void addRoom(const DWORD &roomid);
                void delRoom(const DWORD &roomid);
                void addUser(const uint32_t roomid, const uint32_t userid, uint8_t dev_type);
                void delUser(const DWORD &roomid, const DWORD &userid);
                uint8_t getUserDevicetype(uint32_t user_id);

                void addSpeakUser(const DWORD &roomid, const DWORD &userid);
                void delSpeakUser(const DWORD &roomid, const DWORD &userid);
                void disableMediaData(uint32_t room_id, uint32_t user_id, uint8_t type, bool enable);

                void setUserDataToShare(t_BufferCmdQueue &from, const eUDPProtoType &type);
                void getUserDataFromShare(t_BufferCmdQueue &to, const eUDPProtoType &type);

                void setRelayDataToShare(t_BufferCmdQueue &from, const eUDPProtoType &type);
                void getRelayDataFromShare(t_BufferCmdQueue &to, const eUDPProtoType &type);
                void setRelayDataToShare(t_BufferCmdQueue &from);
                void getRelayDataFromShare(t_BufferCmdQueue &to);

                //tcp处理相关
                bool addOkay(ctVOIPTCPTask *);
                void addRecycle(ctVOIPTCPTask *);

                void clearData();

        private:
                struct stVOIPConfig config_;

                //udp处理相关

                ctVOIPRSThread    *rs_thread_rtp_;
                ctVOIPRelayThread *relay_thread_rtp_;
                t_BufferCmdQueue udp_queue_rtp_1_;      ctMutex mutex_udp_queue_rtp_1_; //rs thread 收到的数据
                t_BufferCmdQueue udp_queue_rtp_2_;      ctMutex mutex_udp_queue_rtp_2_; //relay thread 收到的数据

                ctVOIPRSThread    *rs_thread_rtcp_;
                ctVOIPRelayThread *relay_thread_rtcp_;
                t_BufferCmdQueue udp_queue_rtcp_1_;     ctMutex mutex_udp_queue_rtcp_1_; //rs thread 收到的数据
                t_BufferCmdQueue udp_queue_rtcp_2_;     ctMutex mutex_udp_queue_rtcp_2_; //relay thread 收到的数据

                //tcp处理相关
                ctVOIPTCPConnectThread *tcp_connect_thread_;    //tcp连接处理线程
                ctThreadGroup tcp_okay_threads_;                //处理主线程，多个
                WORD max_tcp_okay_thread_count_;                //最大okay线程数量

                //t_BufferCmdQueue tcp_queue_rtp_1_;    ctMutex mutex_tcp_queue_rtp_1_; //接收的数据
                //t_BufferCmdQueue tcp_queue_rtcp_1_;   ctMutex mutex_tcp_queue_rtcp_1_;
                //t_BufferCmdQueue tcp_queue_rtp_2_;    ctMutex mutex_tcp_queue_rtp_2_; //将要发送的数据
                //t_BufferCmdQueue tcp_queue_rtcp_2_;   ctMutex mutex_tcp_queue_rtcp_2_;
                t_BufferCmdQueue tcp_queue_2_;          ctMutex mutex_tcp_queue_2_;     //将要发送的数据->user

                ctRWLock rwlock_user_;
                std::map<uint32_t, uint8_t> user_dev_type_;

};

#endif //VOIP_THREAD_POOL_H_
